package com.alnex.drools.service;

import com.alnex.drools.pojo.Order;
import com.alnex.drools.pojo.User;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Drools 规则引擎的测试 Demo
 *
 * @author Alnex
 * @date: 2020/11/2 9:54
 */
public class DroolsSimpleDemo {

    /**
     * 计算额外积分金额 规则如下: 订单原价金额
     * 100以下, 不加分
     * 100-500 加100分
     * 500-1000 加500分
     * 1000 以上 加1000分
     */
    public static void main(final String[] args) {
        // KieServices is the factory for all KIE services
        KieServices kieServices = KieServices.Factory.get();

        // From the kie services, a container is created from the classpath
        KieContainer kieContainer = kieServices.getKieClasspathContainer();

        // From the container, a session is created based on
        // its definition and configuration in the META-INF/kmodule.xml file
        KieSession kieSession = kieContainer.newKieSession("pointRules");

        // 初始化数据
        List<Order> orderList = getInitData();

        for (Order order : orderList) {
            kieSession.insert(order);
            kieSession.fireAllRules();

            // 执行相关规则后，执行相关业务代码
            System.out.println("用户" + order.getUser().getName() + "享受额外增加积分: " + order.getScore());

        }
        kieSession.dispose();
    }

    // 初始化数据
    private static List<Order> getInitData() {
        List<Order> orderList = new ArrayList<Order>(4);
        {
            Order order = new Order();
            order.setAccount(50);
            order.setBookingData(LocalDateTime.now());
            User user = new User();
            user.setLevel(1);
            user.setName("AA");
            order.setUser(user);

            orderList.add(order);
        }
        {
            Order order = new Order();
            order.setAccount(80);
            order.setBookingData(LocalDateTime.now());
            User user = new User();
            user.setLevel(1);
            user.setName("AAA");
            order.setUser(user);
            order.setScore(111);

            orderList.add(order);
        }
        {
            Order order = new Order();
            order.setAccount(200);
            order.setBookingData(LocalDateTime.now());
            User user = new User();
            user.setLevel(2);
            user.setName("BBB");
            order.setUser(user);

            orderList.add(order);
        }
        {
            Order order = new Order();
            order.setAccount(700);
            order.setBookingData(LocalDateTime.now());
            User user = new User();
            user.setLevel(3);
            user.setName("CCC");
            order.setUser(user);

            orderList.add(order);
        }
        {
            Order order = new Order();
            order.setAccount(1200);
            order.setBookingData(LocalDateTime.now());
            User user = new User();
            user.setLevel(4);
            user.setName("DDD");
            order.setUser(user);

            orderList.add(order);
        }

        return orderList;
    }
}
